

FILE_INPUT_PED1="siblings.lst" 
FILE_INPUT_PED2="parchild.lst"

FILE_INPUT_NAME ="PopIndex.txt"
FILE_INPUT_PROB1 ="x.pc.txt"
FILE_INPUT_PROB2 ="x.sb.txt"
FILE_OUTPUT_LK ="lk.ssc.txt"
FILE_OUTPUT_X ="ssc.txt"

pair1<-scan(FILE_INPUT_PED1 , list(id1="",id2="")) #Parent-Child, directed network: search for the overlap of 2nd column of both lists.
pair2<-scan(FILE_INPUT_PED2 , list(id1="",id2="")) #Parent-Child
idx<-scan(FILE_INPUT_NAME , list(id=""))

pair.prob1<-read.table(FILE_INPUT_PROB1)
pair.prob2<-read.table(FILE_INPUT_PROB2)


trio.list=matrix(0,length(pair1$id1),6) #col: common.id, id1, id2, pair.prob1, pair.prob2 

#dim(trio.list) <- c(nx, ny)
for ( i in 1:length(pair1$id1))  
{
	j=pmatch(pair1$id2[i],pair2$id1) #s-common s, child-common s
        #which(pair1$id2[i] %in% pair2$id2[]) #Position of common id in list2
	trio.list[i,1]=pair1$id2[i] #common sb
	trio.list[i,2]=pair1$id1[i] 
	trio.list[i,3]=pair2$id2[i]
	trio.list[i,4]=pair.prob1$V1[i]
	trio.list[i,5]=pair.prob2$V1[j]
	trio.list[i,6]=j
}

write.table(trio.list, file = FILE_OUTPUT_X,col.names=F,row.names=F)

bivariate.prob=as.numeric(trio.list[,4:5])
dim(bivariate.prob) <- c(length(bivariate.prob)/2, 2)
#Generate models
pcdf<-mecdf (na.omit(bivariate.prob), continuous=FALSE, validate=TRUE,  project=FALSE, expandf=0.1)


#Use the model to predict the likelihood
for(i in 1:length(trio.list[,1])) 
{
		trio.list[i,6]<-pcdf(matrix( c(trio.list[i,4],trio.list[i,5]), ncol=2))	
}

write.table(trio.list, file = FILE_OUTPUT_LK,col.names=F,row.names=F,sep=",")


